package com.lagou;

import com.lagou.servlet.Servlet;

import java.util.List;

public class Mapper {

    private final List<Host> hosts;
    public Mapper(List<Host> hosts) {
        this.hosts = hosts;
    }

    public List<Host> getHosts() {
        return hosts;
    }

    /**
     * wrapper,
     */
    public static class Wrapper extends MapElement {
        final String urlPattern;
        final Servlet servlet;
        public Wrapper(String name, String urlPattern, Object servlet) {
            super(name, servlet);
            this.urlPattern = urlPattern;
            this.servlet = (Servlet) servlet;
        }

        public String getUrlPattern() {
            return urlPattern;
        }

        public Servlet getServlet() {
            return servlet;
        }
    }

    /**
     * host, 里面有多个context
     */
    public static class Host extends MapElement {
        final String name;
        final List<Context> contexts;
        public Host(String name, List<Context> contexts) {
            super(name, contexts);
            this.name = name;
            this.contexts = contexts;
        }

        public List<Context> getContexts() {
            return contexts;
        }

    }

    /**
     * context, 里面有多个wrapper
     */
    public static class Context extends MapElement {
        final String name;
        final List<Wrapper> wrappers;
        final String appBase;
        public Context(String name, List<Wrapper> wrappers, String appBase) {
            super(name, null);
            this.name = name;
            this.wrappers = wrappers;
            this.appBase = appBase;
        }

        public String getName() {
            return name;
        }

        public List<Wrapper> getWrappers() {
            return wrappers;
        }

        public String getAppBase() {
            return appBase;
        }
    }

    /**
     * 模范tomcat mapElement
     */
    protected abstract static class MapElement {

        public final String name;
        public final Object object;

        public MapElement(String name, Object object) {
            this.name = name;
            this.object = object;
        }
    }
}
